package com.example.demo.RedisTest.UsageCollection;

import com.example.demo.RedisTest.config.RedisConstant;
import com.example.demo.RedisTest.config.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;

/**
 * @author FF998
 */
@Service
@Slf4j
public class RedisUsageServiceImpl implements RedisUsageService {

    @Autowired
    RedisUtils redisUtils;

    @Override
    public Boolean raffleAddUser(List<String> userList) {
        if (userList.size() > 0) {
            String keyRafflePrefix = RedisConstant.KEY_RAFFLE_PREFIX;
            Long addSize = redisUtils.sAdd(keyRafflePrefix, userList.toArray(new String[0]));
            return addSize > 0;
        }
        return false;
    }

    @Override
    public String raffleGetUser(long num) {
        String keyRafflePrefix = RedisConstant.KEY_RAFFLE_PREFIX;
        Long size = redisUtils.sSize(keyRafflePrefix);
        if (size < 1) {
            return "人数不足一人!";
        }
        // 可重复
        // List list = redisUtils.sRandomMembers(keyRafflePrefix, num);

        // 不可重复
        Set<Object> list = redisUtils.sDistinctRandomMembers(keyRafflePrefix, num);
        return "活动：\"抽奖测试\" 的幸运中奖用户是：" + list;
    }

    @Override
    public String raffleGetDeleteUser(long num) {
        String keyRafflePrefix = RedisConstant.KEY_RAFFLE_PREFIX;
        Long size = redisUtils.sSize(keyRafflePrefix);
        if (size < 1) {
            return "人数不足一人!";
        }
        List list = redisUtils.sPop(keyRafflePrefix, num);
        return "活动：\"抽奖测试\" 的幸运中奖用户是：" + list;
    }

    @Override
    public Boolean rankingAddData() {
        String keyRanking = RedisConstant.KEY_RANKING;

        List<String> myList = new ArrayList<>();
        myList.add("AA");
        myList.add("BB");
        myList.add("CC");
        myList.add("DD");
        myList.add("EE");
        myList.add("FF");

        for (int i = 0; i < 5; i++) {
            // 创建 Random 对象
            Random random = new Random();

            // 生成随机索引
            int randomIndex = random.nextInt(myList.size());
            // 获取随机元素
            String randomElement = myList.get(randomIndex);

            // 生成 0 到 100 之间的随机浮点数
            double randomNumber = random.nextDouble() * 100;
            double roundedNum = Math.round(randomNumber * 100) / 100.0;
            redisUtils.zIncrementScore(keyRanking, randomElement, roundedNum);
        }
        return true;
    }

    @Override
    public List rankingSort(String type, long num) {
        String keyRanking = RedisConstant.KEY_RANKING;

        Set<ZSetOperations.TypedTuple<Object>> typedTuples;
        String asc = "ASC";
        if (asc.equals(type)) {
            typedTuples = redisUtils.zRangeWithScores(keyRanking, 0, num != -1 ? num - 1 : num);
        } else {
            typedTuples = redisUtils.zReverseRangeWithScores(keyRanking, 0, num != -1 ? num - 1 : num);
        }
        return new ArrayList<>(typedTuples);
    }

}
